package com.edroid.autorunner;

import java.io.File;
import java.util.List;
import java.util.Map.Entry;
import java.util.Random;

import android.content.ContentResolver;
import android.content.Context;
import android.content.IContentProvider;
import android.content.Intent;
import android.support.v4.os.EConstants;

import com.chrisplus.rootmanager.RootManager;
import com.example.service.SystemServiceGetter;
import com.example.utils.KvTool;
import com.example.utils.Logger;
import com.example.utils.NativeProcessScaner;
import com.example.utils.NativeProcessScaner.NProc;

public class Common {
	static final Logger log = Logger.create("Common");

	/**
	 * 清除程序设置的 key
	 * 
	 * @param context
	 */
	@SuppressWarnings("unused")
	public static void clearSettings(Context context) {
		KvTool kv = new KvTool(new File(EConstants.SETTINGS_SYSTEM));
		ContentResolver cr = context.getContentResolver();

		for(Entry<Object, Object> e : kv.entrySet()) {
			String key = e.getKey().toString();
			String valueType = e.getValue().toString();
			
			log.e("Settings_System", "reset " + key);
			{
				if(String.class.getSimpleName().equals(valueType)) {
					android.provider.Settings.System.putString(cr, key, null);
				} else if(Integer.class.getName().equals(valueType)) {
					android.provider.Settings.System.putInt(cr, key, 0);
				} else if(Long.class.getName().equals(valueType)) {
					android.provider.Settings.System.putLong(cr, key, 0);
				} else if(Float.class.getName().equals(valueType)) {
					android.provider.Settings.System.putFloat(cr, key, 0);
				}
			}
		}
	}
	
	/**
	 * 杀死僵尸进程
	 */
	public static void killGosts(String app) {
		List<NProc> procs = NativeProcessScaner.scanProcess();
		if (procs != null) {
			for (NProc proc : procs) {
				if (proc.processName.startsWith(app)
						|| proc.processName.equals("com.qihoo.daemon")
						|| proc.processName.startsWith("com.baidu.map")) {
					log.e("kill ghost: " + proc.toString());
					SystemServiceGetter.getPermissionService().killProcess(proc.pid);
				}
			}
		}
	}
	
	public static void toDesktop(Context context) {
//	    Intent intent = new Intent(Intent.ACTION_MAIN);
//      intent.addCategory(Intent.CATEGORY_HOME);
        
        Intent intent = new Intent(context.getApplicationContext(), AutoRunActivity.class);
        
        intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);

		context.getApplicationContext().startActivity(intent);
	}
	
	public static int randomAppTime() {
		Random random = new Random();
		int appTime = 0;

		float d = random.nextFloat() * 100;
		if (d < 13.5)
			appTime = 1 + random.nextInt(3);
		else if (d < 24.9)
			appTime = 4 + random.nextInt(7);
		else if (d < 41.2)
			appTime = 11 + random.nextInt(20);
		else if (d < 53.7)
			appTime = 31 + random.nextInt(30);
		else if (d < 71.6)
			appTime = 61 + random.nextInt(100);
		else if (d < 85.7)
			appTime = 190 + random.nextInt(400);
		else if (d < 94)
			appTime = 660 + random.nextInt(1000);
		else
			appTime = 1900 + random.nextInt(5000);

		return appTime;
	}

}
